package com.example.demo.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Goods;
import com.example.demo.entity.Lifecycle;
import com.example.demo.mapper.GoodsMapper;
import com.example.demo.mapper.LifecycleMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import java.util.Date;

import static cn.hutool.core.io.unit.DataSizeUtil.format;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    GoodsMapper goodsMapper;
    @Resource
    LifecycleMapper lifecycleMapper;

    @GetMapping("/change-username")
    public Result setCookie(HttpServletResponse response) {
        // 创建一个 cookie对象

        Cookie cookie = new Cookie("username", "Jovan");
        cookie.setPath("/");   //
        cookie.setMaxAge(24*60*60);       //存活一天
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.addCookie(cookie);
        //将cookie对象加入response响应

        return Result.success("Username is changed!");
    }


    @GetMapping("/new")
    public Result findnew(@RequestParam(defaultValue = "1") Integer pageNum,
                          @RequestParam(defaultValue = "40") Integer pageSize,
                          @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<Goods> wrapper = Wrappers.<Goods>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Goods::getWhichstore, search);
        }
        Page<Goods> goodsPage = goodsMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(goodsPage);
    }

    @GetMapping("/device")
    public Result getdevice(@RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "40") Integer pageSize,
                            @RequestParam(defaultValue = "") String search,
                            @RequestParam(defaultValue = "") String device) {
        LambdaQueryWrapper<Goods> wrapper = Wrappers.<Goods>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Goods::getWhichstore, search);
        }
        if (StrUtil.isNotBlank(device)) {
            wrapper.like(Goods::getName, device);
        }
        Page<Goods> goodsPage = goodsMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(goodsPage);
    }

    @GetMapping("/old")
    public Result findold(@RequestParam(defaultValue = "1") Integer pageNum,
                          @RequestParam(defaultValue = "40") Integer pageSize,
                          @RequestParam(defaultValue = "IT资产旧设备库") String search) {
        LambdaQueryWrapper<Goods> wrapper = Wrappers.<Goods>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Goods::getWhichstore, search);
        }
        Page<Goods> goodsPage = goodsMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(goodsPage);
    }

    @GetMapping("/scrap")
    public Result findscrap(@RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "40") Integer pageSize,
                            @RequestParam(defaultValue = "IT资产报废库") String search) {
        LambdaQueryWrapper<Goods> wrapper = Wrappers.<Goods>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Goods::getWhichstore, search);
        }
        Page<Goods> goodsPage = goodsMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(goodsPage);
    }


    @GetMapping
    public Result findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "40") Integer pageSize,
                           @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<Goods> wrapper = Wrappers.<Goods>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Goods::getName, search);
        }
        Page<Goods> goodsPage = goodsMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(goodsPage);
    }

    @PostMapping
    public Result save(@RequestBody Goods goods) {
//        if (goods.getPassword() == null){
//            user.setPassword("123456");
//        }
//        if (user.getSex() == null){
//            return Result.error("-1","性别不能为空");
//        }
        String now = DateUtil.now();
        goods.setTime(now);
        goodsMapper.insert(goods);
        return Result.success();
    }
    @PostMapping("/delete")
    public Result delete(@RequestBody Goods goods) {
//        if (goods.getPassword() == null){
//            user.setPassword("123456");
//        }
//        if (user.getSex() == null){
//            return Result.error("-1","性别不能为空");
//        }
        goodsMapper.deleteById(goods.getId());
        return Result.success();
    }


    @PutMapping
    public Result update(@RequestBody Goods goods) {
        LambdaQueryWrapper<Goods> wrapper = Wrappers.<Goods>lambdaQuery();
        wrapper.eq(Goods::getRfid, goods.getRfid());
        Goods newgoods = goodsMapper.selectOne(wrapper);
//        刷新生命周期表
        Lifecycle lifecycle = new Lifecycle();
        lifecycle.setGcode(newgoods.getCode());
        lifecycle.setGname(newgoods.getName());
        lifecycle.setChangestoreout(newgoods.getWhichstore());
        newgoods.setWhichstore(goods.getWhichstore());
        newgoods.setWhichstaff(goods.getWhichstaff());
        lifecycle.setTime(DateUtil.date().toStringDefaultTimeZone());
        lifecycle.setChangestorein(goods.getWhichstore());
        goodsMapper.updateById(newgoods);
        lifecycleMapper.insert(lifecycle);
        return Result.success();
    }


}
